package com.cskaoyan.mall.service.lmpl;

import com.cskaoyan.mall.dao.OrderMapper;
import com.cskaoyan.mall.model.bo.order.AddCartOrderBo;
import com.cskaoyan.mall.model.bo.order.ChangeOrderBo;
import com.cskaoyan.mall.model.bo.order.CriticismBo;
import com.cskaoyan.mall.model.bo.order.OrderMessageBo;
import com.cskaoyan.mall.model.po.Criticism;
import com.cskaoyan.mall.model.po.GoodsAndSpec;
import com.cskaoyan.mall.model.po.Order;
import com.cskaoyan.mall.model.po.User;
import com.cskaoyan.mall.model.vo.order.*;
import com.cskaoyan.mall.service.OrderService;
import com.cskaoyan.mall.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: OrderServiceImpl
 * @Description: TODO
 * @Auther 庄左
 * @Date 2022/7/1 16:38
 * @Version
 **/
public class OrderServiceImpl implements OrderService {
    /**
     * @Author: 庄左
     * @Description: 查询所有订单
     * @DateTime: 2022/7/1 17:09
     */
    public List<OrderMessageVo> selectOrderMessage(OrderMessageBo orderMessageBo) {
        //获取mapper对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        List<OrderMessageVo> orderMessage;
        if ("".equals(orderMessageBo.getId())) {
            orderMessage = mapper.selectOrderMessage(new Order(null, orderMessageBo.getMoneyLimit1(), orderMessageBo.getMoneyLimit2(), orderMessageBo.getGoods(), orderMessageBo.getAddress(), orderMessageBo.getName(), orderMessageBo.getState()));
        } else {
            orderMessage = mapper.selectOrderMessage(new Order(Integer.valueOf(orderMessageBo.getId()), orderMessageBo.getMoneyLimit1(), orderMessageBo.getMoneyLimit2(), orderMessageBo.getGoods(), orderMessageBo.getAddress(), orderMessageBo.getName(), orderMessageBo.getState()));
        }
        // Integer id = Integer.parseInt(orderMessageBo.getId().trim());

        sqlSession.commit();
        sqlSession.close();
        return orderMessage;
    }

    /**
     * @Author: 庄左
     * @Description: 查询订单中的用户信息
     * @DateTime: 2022/7/1 17:14
     */
    public UserOrderMessageVo selectUserMessage(Integer id, Integer orderId) {
        //获取mapper对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        UserOrderMessageVo user = mapper.selectUserMessage(id, orderId);
        sqlSession.commit();
        sqlSession.close();
        return user;
    }

    @Override
    public List<OrderSpec> selectSpecList(Integer id) {
        //获取mapper对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        List<OrderSpec> spec = mapper.selectSpecList(id);
        sqlSession.commit();
        sqlSession.close();
        return spec;
    }

    @Override
    public List<OrderStates> selectStateList() {
        //获取mapper对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        List<OrderStates> states = mapper.selectStateList();
        sqlSession.commit();
        sqlSession.close();
        return states;
    }

    @Override
    public CurSpec selectCurSpec(Integer id) {
        //获取mapper对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        CurSpec curSpec = mapper.selectCurSpec(id);
        sqlSession.commit();
        sqlSession.close();
        return curSpec;
    }

    @Override
    public CurState selectCurState(Integer id) {
        //获取mapper对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        CurState curState = mapper.selectCurState(id);
        sqlSession.commit();
        sqlSession.close();
        return curState;
    }

    @Override
    public OrderUpdate selectorderUpdate(Integer id) {
        //获取mapper对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);

        OrderUpdate orderUpdate = mapper.selectorderUpdate(id);

        sqlSession.commit();
        sqlSession.close();
        return orderUpdate;
    }


    @Override
    public String selectStateById(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        String state = mapper.selectStateById(id);
        sqlSession.commit();
        sqlSession.close();
        return state;
    }

    @Override
    public String selectSpecById(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        String spec = mapper.selectSpecById(id);
        sqlSession.commit();
        sqlSession.close();
        return spec;
    }

    @Override
    public void changeOrder(ChangeOrderBo changeOrderBo) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);


        Order order = new Order(changeOrderBo.getId(), changeOrderBo.getState(), changeOrderBo.getStateValue(), changeOrderBo.getNum().toString(), changeOrderBo.getSpecValue(), changeOrderBo.getSpec());

        //
        mapper.changeOrder(order);

        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteOrder(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        mapper.deleteOrder(id);
        sqlSession.commit();
        sqlSession.close();
    }

    public Integer selectGoodsIdByOrderId(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        Integer goodsId = mapper.selectGoodsIdByOrderId(id);
        sqlSession.commit();
        sqlSession.close();
        return goodsId;
    }


    //购物车中的订单及商品信息
    // @Override
    // public CartGoodVo selectCartGoods(Integer orderId) {
    //     SqlSession sqlSession = MyBatisUtils.getSqlSession();
    //     OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
    //
    //     CartGoodVo cartGoodVo = mapper.selectCartGoodByOrderId(orderId);
    //     sqlSession.commit();
    //     sqlSession.close();
    //     return cartGoodVo;
    // }

    @Override
    public List<CartOrderVo> selectCartOrderByStateId(Integer stateId,String name) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        List<CartOrderVo> cartOrderVos = mapper.selectCartOrderByStateId(stateId,name);
        sqlSession.commit();
        sqlSession.close();
        return cartOrderVos;
    }

    //根据发货状态id，拿到发货状态stateValue 返回一个String
    @Override
    public String selectStateValueByStateId(Integer stateId) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        String state = mapper.selectStateValueByStateId(stateId);
        sqlSession.commit();
        sqlSession.close();
        return state;
    }

    //根据规格id拿到 规格spec，商品id
    @Override
    public GoodsAndSpec selectGoodsIdAndSpecMessageBySpecId(Integer specId) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        GoodsAndSpec goodsIdAndSpec = mapper.selectGoodsIdAndSpecMessageBySpecId(specId);
        sqlSession.commit();
        sqlSession.close();
        return goodsIdAndSpec;
    }

    //根据商品id拿到商品的名称和图片 goods img
    @Override
    public GoodsAndSpec selectGoodsMessageByGoodsId(Integer goodsId) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        GoodsAndSpec goodsMessage = mapper.selectGoodsMessageByGoodsId(goodsId);
        sqlSession.commit();
        sqlSession.close();
        return goodsMessage;
    }

    //根据昵称拿到用户的id，用户名，收件人，地址，电话
    @Override
    public User selectUserMessageByUserNickName(String nikeName) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        User userMessage = mapper.selectUserMessageByUserNickName(nikeName);
        sqlSession.commit();
        sqlSession.close();
        return userMessage;
    }

    @Override
    public void insertGoodsToCart(AddCartOrderBo addCartOrderBo, String state, GoodsAndSpec goodsAndSpec, GoodsAndSpec goodsAndSpec1, User user) {
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        mapper.insertGoodsToCart(new Order(null, user.getId(), addCartOrderBo.getToken(), user.getRecipient(), user.getAddress(), user.getPhone(), goodsAndSpec1.getGoodsName(), goodsAndSpec.getGoodsid(), goodsAndSpec.getSpec(), addCartOrderBo.getGoodsDetailId(), addCartOrderBo.getNum().toString(), addCartOrderBo.getAmount().toString(), addCartOrderBo.getState(), format, false, state, user.getUsername(), goodsAndSpec1.getImg()));
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void updateStockNumInSpec(Integer stockNum, Integer specId) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        mapper.updateStockNumInSpec(stockNum, specId);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void updateOrderStateIdAndState() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        mapper.updateOrderStateIdAndState();
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void updateOrderStateByOrderIdFromPay(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        mapper.updateOrderStateByOrderIdFromPay(id);
        sqlSession.commit();
        sqlSession.close();
    }


    //插入评论信息
    public int insertCriticsm(CriticismBo criticismBo) {
        //根据规格id查规格的名字
        String spec = selectSpecById(criticismBo.getGoodsDetailId());
        //根据昵称查找用户id


        Integer userId = Integer.valueOf(selectUserIdByUserNickName(criticismBo.getToken()));

        Criticism criticism = new Criticism();


        criticism.setOrderId(criticismBo.getOrderId());
        criticism.setSpecId(criticismBo.getGoodsDetailId());
        criticism.setGoodsId(criticismBo.getGoodsId());
        criticism.setScore(criticismBo.getScore());
        criticism.setComments(criticismBo.getContent());
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        criticism.setTime(format);
        criticism.setUserId(userId);
        criticism.setSpecName(spec);
        criticism.setUserNickName(criticismBo.getToken());

        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        int i = mapper.insertCriitism(criticism);
        sqlSession.commit();
        sqlSession.close();
        return i;


    }

    public String selectUserIdByUserNickName(String userNickName) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        String userId = mapper.selectUserIdByUserNickName(userNickName);
        sqlSession.commit();
        sqlSession.close();

        return userId;
    }

    @Override
    public int updateOrderHasComment(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        int orderNum = mapper.updateOrderHasComment(id);
        sqlSession.commit();
        sqlSession.close();
        return orderNum;
    }

    @Override
    public int updateOrderStateByOrderIdFromConfirmReceive(Integer id) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
        int orderNum = mapper.updateOrderStateByOrderIdFromConfirmReceive(id);
        sqlSession.commit();
        sqlSession.close();
        return orderNum;
    }
}
